﻿
/*
*      
*          ┌─┐       ┌─┐
*       ┌──┘ ┴───────┘ ┴──┐
*       │                 │
*       │       ───       │
*       │  ─┬┘       └┬─  │
*       │                 │
*       │       ─┴─       │
*       │                 │
*       └───┐         ┌───┘
*           │         │
*           │         │
*           │         │
*           │         └──────────────┐
*           │                        │
*           │                        ├─┐
*           │                        ┌─┘    
*           │                        │
*           └─┐  ┐  ┌───────┬──┐  ┌──┘         
*             │ ─┤ ─┤       │ ─┤ ─┤         
*             └──┴──┘       └──┴──┘ 
*                 神兽保佑 
*                 代码无BUG! 
*  Author shang
*  2022-07-21 15:56:00
*/

using System.Security.Cryptography;
using System.Text;
using Quartz.Impl.Triggers;

namespace TS.Common;

public static class StringExtension
{
    /// 判断字符串是否为空
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static bool IsNullOrEmpty(this string s)
    {
        return string.IsNullOrEmpty(s) || s == "null";
    }



    public static int ToInt(this string s)
    {
        int.TryParse(s, out var res);
        return res;
    }
    
    public static string ConverBase64String(this string str)
    {
        return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
    }

    public static string Base64Decode(this string Message)
    {
        byte[] bytes = Convert.FromBase64String(Message);
        return Encoding.UTF8.GetString(bytes);
    }

    public static string HMACSHA256(this string message, string secret)
    {
        secret = secret ?? "";
        byte[] keyByte = Encoding.UTF8.GetBytes(secret);
        byte[] messageBytes = Encoding.UTF8.GetBytes(message);
        using (var hmacsha256 = new HMACSHA256(keyByte))
        {
            byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            return Convert.ToBase64String(hashmessage);
        }
    }
    
    public static (bool, string) IsValidExpression(this string cronExpression)
    {
        try
        {
            CronTriggerImpl trigger = new CronTriggerImpl();
            trigger.CronExpressionString = cronExpression;
            DateTimeOffset? date = trigger.ComputeFirstFireTimeUtc(null);
            return (date != null, date == null ? $"请确认表达式{cronExpression}是否正确!" : "");
        }
        catch (Exception e)
        {
            return (false, $"请确认表达式{cronExpression}是否正确!{e.Message}");
        }
    }
    public static string ToMD5(this string s)
    {
        return ToMD5(s, Encoding.UTF8);
    }
    public static string ToMD5(this string s, Encoding encoding)
    {
        using (var md5 = System.Security.Cryptography.MD5.Create())
        {
            var inputBytes = encoding.GetBytes(s);
            var hashBytes = md5.ComputeHash(inputBytes);

            var sb = new StringBuilder();
            foreach (var hashByte in hashBytes)
            {
                sb.Append(hashByte.ToString("X2"));
            }
            return sb.ToString();
        }
    }
}